/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testblind;

import java.math.BigInteger;
import java.security.SecureRandom;

/**
 *
 * @author Maciek
 */
public class TestBlind {
   
    
    public static void main(String[] args) {
        
        try {
            
            //*********************** SETUP **********************************
            
            PKW pkw = new PKW(4*1024);
            StringBuilder sb = new StringBuilder();
            int z = 500;
            for(int i = 0 ; i < z ; ++ i){
                sb.append(i%10);
            }
            String message = sb.toString();
            byte [] raw = message.getBytes("UTF8");
            
            BigInteger m = new BigInteger(raw);
            BigInteger e = pkw.pubKey.getPublicExponent();
        
 
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG","SUN");
            byte [] randomBytes = new byte[10];
            BigInteger r = null;
            BigInteger n = pkw.pubKey.getModulus();
            BigInteger gcd = null;
            BigInteger one = BigInteger.ONE;
            //check that gcd(r,n) = 1 && r < n && r > 1
            do {
                random.nextBytes(randomBytes);
                r = new BigInteger(1, randomBytes);
                gcd = r.gcd(n);
                System.out.println("gcd: " + gcd);
            }
            while(!gcd.equals(one) || r.compareTo(n)>=0 || r.compareTo(one)<=0);
 
           //********************* BLIND ************************************
            
            BigInteger b = ((r.modPow(e,n)).multiply(m)).mod(n);
            System.out.println("\nb = " + b);
            
            //********************* SIGN *************************************
            
            BigInteger bs = pkw.sign(b);
            System.out.println("bs = " + bs);
            
            //********************* UNBLIND **********************************
            
             BigInteger s = r.modInverse(n).multiply(bs).mod(n); // czyli mamy policzone m^privKey.exoponent
            System.out.println("s = " + s);
            
            //********************* VERIFY ***********************************
            
            System.out.println(pkw.verifySignitureUsingPrivateKey(s, m));//(m^d == s)
            
            //try to verify using the RSA formula
          //  BigInteger check = s.modPow(e,n); 
            BigInteger check = pkw.decode(s);
            System.out.println(m.equals(check)) ; // s^e == m bo s = m^d
 
            //BOTH TESTS RETURN FALSE - s must not be a valid signature of m 
            
            byte mbytes[] = check.toByteArray();
            System.out.println(new String(s.toByteArray(),"UTF-8"));
            System.out.println(new String(mbytes, "UTF-8"));
            System.out.println(message.equals(new String(mbytes, "UTF-8")));
        }
        catch(Exception ex) {
            System.out.println("ERROR: ");
            ex.printStackTrace();
        }
    }  
}
